<?php

namespace app\index\controller;

use app\admin\model\Product as ProductMdl;
use app\admin\model\Image;

use think\Controller;

class Product extends Controller
{

    public function share()
    {
        $id = $this->request->param('id');
        if (!$id) {
            return '缺少参数！';
        }

        $model = new ProductMdl();
        $item = $model->find($id);
        if (!$item) {
            return '该商品不存在！';
        }

        // 轮播图
        $item['img_urls'] = explode(',', $item['img_urls']);
        // 单位
        $unitArr = get_product_unit();
        $item['unit'] = $unitArr[$item['unit']];
        // 库存
        $item['qty'] = $model->getStock($item['id']);

        // 详情图
        $detailImgs = Image::where(['related_id' => $item->id, 'tag' => 'detail_imgs'])->field('id, img_url')->order('sorting ASC')->select();
        $item['detailImgs'] = $detailImgs;

        return $this->fetch('', [
            'info' => $item
        ]);
    }
}
